package com.bodu.exchange.client.impl;

import cn.hutool.core.util.ObjectUtil;
import com.bodu.core.entity.result.Result;
import com.bodu.exchange.client.ExchangeBookClient;
import com.bodu.exchange.entity.request.ExchangeBookRequest;
import com.bodu.exchange.entity.request.UserExchangeBookRequest;
import com.bodu.exchange.entity.vo.ExchangeBookVo;
import com.bodu.exchange.enums.ExchangeBookDefaultFlagEnums;
import com.bodu.exchange.service.ExchangeBookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ExchangeBookClientImpl implements ExchangeBookClient {

    @Autowired
    private ExchangeBookService exchangeBookService;

    @Override
    public Result<Long> createExchangeBook(ExchangeBookRequest request) {
        if (ObjectUtil.equals(request.getDefaultFlag(), ExchangeBookDefaultFlagEnums.YES.getValue())) {
            exchangeBookService.updateDefaultFlagByUserId(request.getUserId(), 0);
        }
        return Result.success(exchangeBookService.save(request));
    }

    @Override
    public Result<Long> updateExchangeBook(ExchangeBookRequest request) {
        Long updateCount = exchangeBookService.update(request);
        return Result.success(updateCount);
    }

    @Override
    public Result<Integer> updateDefaultExchangeBookStatus(ExchangeBookRequest request) {
        Integer updateCount = exchangeBookService.updateDefaultExchangeBookStatus(request.getId(), request.getDefaultFlag());
        return Result.success(updateCount);
    }

    @Override
    public Result<ExchangeBookVo> bookList(UserExchangeBookRequest request) {
        ExchangeBookRequest exchangeBookRequest = new ExchangeBookRequest();
        exchangeBookRequest.setUserId(request.getUserId());
        exchangeBookRequest.setPrivateFlag(request.getPrivateFlag());
        List<ExchangeBookVo> list = exchangeBookService.bookList(exchangeBookRequest);
        return Result.success(list);
    }


}
